
open Ast

let rec labelize_list func ident lst = 
	List.map (fun item -> func ident item) lst
	
and labelize_statement ident = function
	| ABlock(block) -> ABlock(labelize_block ident block)
	| AIfStatement(ifStatement) -> AIfStatement(labelize_ifStatement ident ifStatement)
	| AWithStatement(withStatement) -> AWithStatement(labelize_withStatement ident withStatement)
	| ALabelledStatement(labelledStatement) -> ALabelledStatement(labelize_labelledStatement ident labelledStatement)
	| ATryStatement(tryStatement) -> ATryStatement(labelize_tryStatement ident tryStatement)
	| AContinueStatement(stmt) -> labelize_continueStatement ident stmt
	| ABreakStatement(stmt) -> labelize_breakStatement ident stmt
	| a -> a

and labelize_block ident block =
	labelize_list labelize_statement ident block
	
and labelize_ifStatement ident = function
	| AIfElse(expr, stmt1, stmt2) ->
		let stmt1 = labelize_statement ident stmt1 in
		let stmt2 = labelize_statement ident stmt2 in
		AIfElse(expr,stmt1,stmt2)
	| AIf(expr, stmt) ->
		AIf(expr, labelize_statement ident stmt)

and labelize_withStatement ident (expr, stmt) = 
	expr, (labelize_statement ident stmt)

and labelize_tryStatement ident = function
	| ATryCatch(block, catch) ->
		let block = labelize_block ident block in
		let catch = labelize_catch ident catch in
		ATryCatch(block, catch)
	| ATryFinally(block, finally) ->
		let block = labelize_block ident block in
		let finally = labelize_block ident finally in
		ATryFinally(block, finally)
	| ATryCatchFinally(block, catch, finally) ->
		let block = labelize_block ident block in
		let catch = labelize_catch ident catch in
		let finally = labelize_block ident finally in
		ATryCatchFinally(block, catch, finally)

and labelize_catch ident (ident2,block) =
	let block = labelize_block ident block in
	(ident2,block)

and labelize_continueStatement ident = function
	| None -> ABreakStatement(Some ident)
	| Some ident -> AContinueStatement(Some ident)

and labelize_breakStatement ident = function
	| None -> ABreakStatement(Some ident)
	| Some ident -> ABreakStatement(Some ident)
	
and labelize_labelledStatement ident (ident2,stmt) =
	(ident2, labelize_statement ident stmt)
	

